package com.lwj.liwanjia.view.activity.dianpu;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DianPuAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.utils.PopUtils;
import com.lwj.liwanjia.view.activity.search.SearchActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopDetailActivity extends BaseActivity {


    @BindView(R.id.market_back)
    TextView marketBack;
    @BindView(R.id.market_search)
    RelativeLayout marketSearch;
    @BindView(R.id.rlv_detail)
    SuperRecyclerView rlvDetail;
    @BindView(R.id.iv_fenlei)
    ImageView ivFenlei;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_zonghe)
    TextView tvZonghe;
    @BindView(R.id.tv_sales)
    TextView tvSales;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.height_price)
    ImageView heightPrice;
    @BindView(R.id.lower_price)
    ImageView lowerPrice;
    @BindView(R.id.tv_xinpin)
    TextView tvXinpin;

    private DianPuAdapter adapter;
    private List<String> list = new ArrayList<>();
    private TextView mTv;//当前选中的标签
    private String mPrice;//价格（高-低）

    @Override
    protected void initView() {
        tvSearch.setText("搜索店内商品");
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvDetail.setLayoutManager(manager);
        rlvDetail.setRefreshEnabled(false);
        rlvDetail.setLoadMoreEnabled(false);
        rlvDetail.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = getResources().getDimensionPixelOffset(R.dimen.x10);
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.x10);
                if (parent.getChildAdapterPosition(view) % 2 == 1) {
                    outRect.left = getResources().getDimensionPixelOffset(R.dimen.x10);
                }
            }
        });
      //  adapter = new DianPuAdapter(this, list);
        rlvDetail.setAdapter(adapter);
        listener();
        mTv = tvZonghe;
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent i = new Intent(ShopDetailActivity.this, ShangpinDetailActivity.class);
                startActivity(i);
            }
        });
        adapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                Toast.makeText(ShopDetailActivity.this, "添加购物车成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initData() {
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_detail;
    }


    @OnClick({R.id.tv_zonghe, R.id.tv_sales, R.id.tv_price, R.id.height_price, R.id.lower_price, R.id.tv_xinpin, R.id.market_back, R.id.market_search, R.id.iv_fenlei})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.market_back:
                finish();
                break;
            case R.id.market_search://搜索
                Intent intent = new Intent(ShopDetailActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_fenlei://分类
                PopUtils.getInstance().showPopupWindow(this, ivFenlei);
                break;
            case R.id.tv_zonghe://综合
                switchTab(tvZonghe);
                break;
            case R.id.tv_sales://销量
                switchTab(tvSales);
                break;
            case R.id.tv_price://价格
                switchTab(tvPrice);
                heightPrice.setImageResource(R.mipmap.gao);
                lowerPrice.setImageResource(R.mipmap.di);
                mPrice = "从高到低";
                break;
            case R.id.height_price://高价格
                switchTab(tvPrice);
                heightPrice.setImageResource(R.mipmap.gao);
                lowerPrice.setImageResource(R.mipmap.di);
                mPrice = "从高到低";
                break;
            case R.id.lower_price://低价格
                switchTab(tvPrice);
                heightPrice.setImageResource(R.mipmap.gao_1);
                lowerPrice.setImageResource(R.mipmap.di_1);
                mPrice = "从低到高";
                break;
            case R.id.tv_xinpin://新品
                switchTab(tvXinpin);
                break;
        }
    }

    /**
     * 切换tab标签
     *
     * @param tv
     */
    private void switchTab(TextView tv) {
        if (mTv != tv) {
            tv.setTextColor(getResources().getColor(R.color.colorWite));
            tv.setBackgroundColor(getResources().getColor(R.color.colorCECECE));
            mTv.setTextColor(getResources().getColor(R.color.colorC4C4C4));
            mTv.setBackgroundColor(getResources().getColor(R.color.colorWite));
            mTv = tv;
            if (tv != tvPrice) {
                heightPrice.setImageResource(R.mipmap.gao_1);
                lowerPrice.setImageResource(R.mipmap.di);
            }
        }
    }
}
